<?php
class Import_Manager extends CI_Controller{

	protected $_message;

	public function __construct(){
		parent::__construct();
		$this->load->helper('file');
		$this->load->model('import');
	}

	public function import_dir($dir = 'import'){
		//Read files in directory.
		if(!is_dir($dir)){
			echo "Directory not found.";
			return false;
		}

		$files = array_keys(get_dir_file_info($dir));
		$log_message = '';
		foreach($files as $file){
			$info_file = pathinfo($file);
			$table = $info_file['filename'];
			$log_message .= "\n File {$table} . {$info_file['extension']}:";
			if($this->_import_data($dir . '/' . $file, $table)){
				$log_message .= "  - Import file successfully.";
			}
			else{
				$log_message .= "  - Import file unsuccesfully." . $this->_message;
			}
		}
		//Write file and print message output.
		write_file(APPPATH . 'cron/log/log', $log_message);
		echo $log_message;
	}

	public function import_file($path_file = '', $table = '') {
		if(!$this->_import_data($path_file, $table)){
			echo "  - Import file unsuccesfully." . "\n \t " . $this->_message;
		}
		else{
			echo "  - Import file successfully.";
		}
	}

	protected function _import_data($path_file = '', $table = '') {
		$path_file = explode('__', $path_file);
		// import__flc_kaisai.xml
		$path_file = implode('/', $path_file);
		// import/flc_kaisai.xml

		if(!$this->_validate($path_file, $table)){
			return false;
		}

		$structure_file = $this->_get_structure_xml($path_file);

		//Check all item in this structer_file is exist in table's column.
		if(!$this->_valid_file_structure($table, $structure_file)){
			$this->_message .= "\n \t Colunm in this file not equivalent with colunm in this table.";
			return false;
		}

		$this->db->trans_start();

		try{
			//Create file
			$this->import->do_import($path_file, $structure_file, $table);

			return $this->db->trans_complete();
		}
		catch(Exception $e) {
			$this->db->trans_complete();
			$this->_message .= " - \n \t Errors: ".  $e->getMessage() . "\n";

			return false;
		}

		return false;
	}

	private function _get_structure_xml($path_file = ''){
		if(!$path_file){
			$this->_message =  "\n \t Path to file is invalid.";
			return false;
		}

		$xml = simplexml_load_file($path_file);

		return array_keys((array)$xml->children()->children()->children());
	}

	private function _valid_file_structure($table = '', array $structure_file = null){
		if(empty($structure_file) || !$table){
			return false;
		}

		foreach($structure_file as $field){
			if(!$this->import->is_existed_field($table, $field))	{
				return false;
			}
		}

		return true;
	}

	private function _validate($path_file = '', $table = ''){
		if(!$path_file){
			$this->_message = "\n \t Path to file is empty.";
			return false;
		}

		if(!$table){
			$this->_message = "\n \t You not specify the table to import for this file.";
			return false;
		}

		if(!get_file_info($path_file)){
			$this->_message =  "\n \t File '{$path_file}' not found";
			return false;
		}

		$path_info = pathinfo($path_file);
		if($path_info['extension'] != 'xml'){
			$this->_message =  "\n \t File extension must be xml extension.";
			return false;
		}

		//Check this table is existed in database.
		if(!$this->import->is_existed_table($table)){
			$this->_message =  "\n \t Table not found.";
			return false;
		}

		return true;
	}
}
/*
 * LOAD DATA LOCAL INFILE '/home/example/www/kaisaidata.xml'
INTO TABLE flc_kaisai LINES STARTING BY '<row>' TERMINATED BY '</row>'
(@person)
SET    kaisai_id	= 	SUBSTRING(@person,
		@temp:=LOCATE('<kaisai_id>', @person) + LENGTH('<kaisai_id>'),
		LOCATE('</kaisai_id>', @person) - @temp),
kaisai_date = 	SUBSTRING(@person,
		@temp:=LOCATE('<kaisai_date>', @person) + LENGTH('<kaisai_date>'),
		LOCATE('</kaisai_date>', @person) - @temp),
boos_id	 	= 	SUBSTRING(@person,
		@temp:=LOCATE('<boos_id>', @person) + LENGTH('<boos_id>'),
		LOCATE('</boos_id>', @person) - @temp),
boos_name	= 	SUBSTRING(@person,
		@temp:=LOCATE('<boos_name>', @person) + LENGTH('<boos_name>'),
		LOCATE('</boos_name>', @person) - @temp),
syozoku_code 		= 	SUBSTRING(@person,
		@temp:=LOCATE('<syozoku_code>', @person) + LENGTH('<syozoku_code>'),
		LOCATE('</syozoku_code>', @person) - @temp),
syozoku_name	 	= 	SUBSTRING(@person,
		@temp:=LOCATE('<syozoku_name>', @person) + LENGTH('<syozoku_name>'),
		LOCATE('</syozoku_name>', @person) - @temp),
create_datetime = NOW(),
lastup_datetime = NOW();




LOAD DATA LOCAL INFILE '/home/example/www/staffmaster.xml'
INTO TABLE flc_staff LINES STARTING BY '<row>' TERMINATED BY '</row>'
(@staff)
SET    staff_id	= 	SUBSTRING(@staff,
		@temp:=LOCATE('<staff_id>', @staff) + LENGTH('<staff_id>'),
		LOCATE('</staff_id>', @staff) - @temp),
staff_name = 	SUBSTRING(@staff,
		@temp:=LOCATE('<staff_name>', @staff) + LENGTH('<staff_name>'),
		LOCATE('</staff_name>', @staff) - @temp),
staff_kana	 	= 	SUBSTRING(@staff,
		@temp:=LOCATE('<staff_kana>', @staff) + LENGTH('<staff_kana>'),
		LOCATE('</staff_kana>', @staff) - @temp),
staff_job	= 	SUBSTRING(@staff,
		@temp:=LOCATE('<staff_job>', @staff) + LENGTH('<staff_job>'),
		LOCATE('</staff_job>', @staff) - @temp),
busyo_code 		= 	SUBSTRING(@staff,
		@temp:=LOCATE('<busyo_code>', @staff) + LENGTH('<busyo_code>'),
		LOCATE('</busyo_code>', @staff) - @temp),
busyo_name	 	= 	SUBSTRING(@staff,
		@temp:=LOCATE('<busyo_name>', @staff) + LENGTH('<busyo_name>'),
		LOCATE('</busyo_name>', @staff) - @temp),
busyo_kana	 	= 	SUBSTRING(@staff,
		@temp:=LOCATE('<busyo_kana>', @staff) + LENGTH('<busyo_kana>'),
		LOCATE('</busyo_kana>', @staff) - @temp),
syozoku_code	 	= 	SUBSTRING(@staff,
		@temp:=LOCATE('<syozoku_code>', @staff) + LENGTH('<syozoku_code>'),
		LOCATE('</syozoku_code>', @staff) - @temp),
syozoku_name	 	= 	SUBSTRING(@staff,
		@temp:=LOCATE('<syozoku_name>', @staff) + LENGTH('<syozoku_name>'),
		LOCATE('</syozoku_name>', @staff) - @temp),
syozoku_kana	 	= 	SUBSTRING(@staff,
		@temp:=LOCATE('<syozoku_kana>', @staff) + LENGTH('<syozoku_kana>'),
		LOCATE('</syozoku_kana>', @staff) - @temp),
password		= 'awshdfsjdfjsadfhsajkdfs',
create_datetime = NOW(),
lastup_datetime = NOW();
*/
?>